package tools.exceltools;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.handler.WriteHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static java.util.Objects.isNull;

public class ExcelUtils {
    /**
     * 重载export方法，动态列导出Excel表格
     *
     * @param response
     * @param dataList                导出数据
     * @param fileName                文件名
     * @param sheetName               sheetName
     * @param writeHandler            自定义拦截器实现格式
     * @param includeColumnFiledNames 导出列
     * @param exportClass             导出对象class
     * @throws IOException
     */
    public static <T> void exportExcel(HttpServletResponse response, List<T> dataList, String fileName, String sheetName,
                                       WriteHandler writeHandler, Collection<String> includeColumnFiledNames,
                                       Class<T> exportClass) throws IOException {
        if (isNull(includeColumnFiledNames)) {
            //如果没有选择，则默认导出全部列
            exportExcelAllColumn(response, dataList, fileName, sheetName, writeHandler, exportClass);
        } else {
            exportExcelIncludeColumn(response, dataList, fileName, sheetName, writeHandler, includeColumnFiledNames, exportClass);
        }
    }

    /**
     * 没有动态列的全量导出
     *
     * @param response
     * @param dataList
     * @param fileName     * @param sheetName sheetName
     * @param writeHandler
     * @param exportClass  导出类的对象
     * @param <T>
     * @throws IOException
     */
    public static <T> void exportExcelAllColumn(HttpServletResponse response, List<T> dataList, String fileName, String sheetName,
                                                WriteHandler writeHandler, Class<T> exportClass) throws IOException {
        setExcelResponse(response, fileName);

        EasyExcel.write(response.getOutputStream(), exportClass)
            .registerWriteHandler(writeHandler)
            .sheet(sheetName).doWrite(dataList);
    }

    public static <T> void exportExcelExcludeColumn(HttpServletResponse response, List<T> dataList, String fileName, String sheetName,
                                                    WriteHandler writeHandler, Collection<String> excludeColumnFiledNames,
                                                    Class<T> exportClass) throws IOException {
        setExcelResponse(response, fileName);

        EasyExcel.write(response.getOutputStream(), exportClass)
            .excludeColumnFiledNames(excludeColumnFiledNames)
            .registerWriteHandler(writeHandler)
            .sheet(sheetName).doWrite(dataList);
    }

    public static <T> void exportExcelIncludeColumn(HttpServletResponse response, List<T> dataList, String fileName, String sheetName,
                                                    WriteHandler writeHandler, Collection<String> includeColumnFiledNames,
                                                    Class<T> exportClass) throws IOException {
        setExcelResponse(response, fileName);

        EasyExcel.write(response.getOutputStream(), exportClass)
            .includeColumnFiledNames(includeColumnFiledNames)
            .registerWriteHandler(writeHandler)
            .sheet(sheetName).doWrite(dataList);
    }

    private static void setExcelResponse(HttpServletResponse response, String fileName) {
        //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setContentType("application/binary;charset=utf-8");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + DateUtil.formatTime(new Date()) + ".xlsx");
    }
}
